--
-- Author: ping.wang
-- Date: 2018-09-19 17:35:53
--
-- CrystalView 强化水晶界面
--

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)
local IconRender = require("app.widget.renders.IconRender")
local MakeLoadingRender = import(".renders.MakeLoadingRender")
local string_split = string.split
local table_insert = table.insert
local string_format = string.format

ClassRef.VIEW_ROOT_RES = Res.CrystalStrengthenView

function ClassRef:init()

    display.nodeRemoveForNotHit(self._ui.Panel, function()

        self:performWithDelay(function()
            self:closeView(false)
        end, 0)

    end)

    self._ui.Button_close:addClickEventListener(function()
        self:closeView(false)
    end)

    self._materialIcons = {}    --材料icon
    self._makeLoadings = {}    --进度条
    for i = 1, 3 do
        local icon = IconRender.create(self._ui["Panel/Panel_icon/icon_"..i])
        table_insert(self._materialIcons, icon)

        local makeloading = MakeLoadingRender.create(self._ui["Panel/Panel_icon/bg_"..i])
        table_insert(self._makeLoadings, makeloading)
    end

    self._crystalIcon = IconRender.create(self._ui["Panel/Panel_icon/icon"])
    self._crystalIcon:setNumBgVisible(false)
    self._crystalIcon:setTipEnable(true)
    self._ui.btn_s:addClickEventListener(function()
        --判断所需要的材料是否足够
        if self._isMaterialEnough then
            executeFunc(self._onOperate, "strengthCrystal",self._data)
        else
            display.pushToast(L("tips_2000009"))
        end
    end)

    self:refresh()
end

--data = {itemUid=, itemId=, isOnEquip=, equipUid=}
function ClassRef:refresh()
    local rockId = self._data.itemId

    local rockInfo = GD:queryRockById(rockId)
    self._crystalIcon:setData(UD:parseLootToBagItem({type = "item",id = tonumber(rockId), num = 1 }))
    self._crystalIcon:setLevelEx(tonumber(rockInfo.leve))
    self._isMaterialEnough = false --材料是否足够
    if rockInfo then
        if rockInfo.leveupNeedID then
            --没有到达最高级
            self._ui.Panel_data_max:setVisible(false)
            self._ui.Panel_max:setVisible(false)
            self._ui.Panel_data:setVisible(true)
            self._ui.btn_s:setVisible(true)

            self._isMaterialEnough = true --材料是否足够

            local needDatas = string_split(rockInfo.leveupNeedID,";")
            for i = 1, 3 do
                local itemData = UD:parseRewardToBagItem(needDatas[i])
                local materialInfo = string_split(needDatas[i],"|")
                local needNum = tonumber(materialInfo[3])
                local icon = self._materialIcons[i]
                icon:setVisible(true)
                local existNum = self:getNumCount(itemData)

                icon:setData(itemData)
                icon:setNumString(string_format("%d",needNum))
                icon:setTipEnable(true)
                if existNum >= needNum then
                    icon:setNumberColor(cc.c3b(255,204,0))
                else
                    icon:setNumberColor(cc.c3b(255,0,0))
                    self._isMaterialEnough = false
                end

                self._makeLoadings[i]:setData(tonumber(existNum * 100 / needNum))
            end

            self._ui["Panel/Panel_icon/Image_light"]:setVisible(self._isMaterialEnough)

            self._ui["Panel/Panel_data/lv_1"]:setString(string_format(L("skill_lv"),tonumber(rockInfo.leve)))
            local zhuanjingStr = L(string_format("zhuanjing_%d",tonumber(rockInfo.type)))
            self._ui["Panel/Panel_data/tlt_1"]:setString(string_format("%s:%s",zhuanjingStr,tostring(rockInfo.showValue)))
            --self._ui["Panel/Panel_data/num_1"]:setString(tostring(rockInfo.showValue))
            local nextRockInfo = GD:queryRockById(tonumber(rockInfo.levelupId))
            if nextRockInfo then
                self._ui["Panel/Panel_data/lv_2"]:setString(string_format(L("skill_lv"),tonumber(nextRockInfo.leve)))
                zhuanjingStr = L(string_format("zhuanjing_%d",tonumber(nextRockInfo.type)))
                self._ui["Panel/Panel_data/tlt_2"]:setString(string_format("%s:%s",zhuanjingStr,tostring(nextRockInfo.showValue)))
                --self._ui["Panel/Panel_data/num_2"]:setString(tostring(nextRockInfo.showValue))
            else
                dump("valid rockid ".. rockId + 1)
            end

        else
            --到达了最高级
            self._ui.Panel_data_max:setVisible(true)
            self._ui.Panel_max:setVisible(true)
            self._ui.Panel_data:setVisible(false)
            self._ui.btn_s:setVisible(false)

            for i = 1, 3 do
                self._materialIcons[i]:setVisible(false)
            end

            self._ui["Panel/Panel_data_max/lv_1"]:setString(string_format(L("skill_lv"),tonumber(rockInfo.leve)))
            self._ui["Panel/Panel_data_max/tlt_1"]:setString(L(string_format("zhuanjing_%d",tonumber(rockInfo.type))))
            self._ui["Panel/Panel_data_max/num_1"]:setString(tostring(rockInfo.showValue))
        end
    end
end

function ClassRef:getNumCount(itemData)
    local iCount = 0
    if itemData.itemBagType == Enums.ItemBagType.item then -- 物品
        iCount = UD:getItemAmountByTid(itemData.tid)
    elseif itemData.itemBagType == Enums.ItemBagType.daibi then -- 货币
        iCount = UD:getDaibiValue( itemData:getId() )
    end
    return iCount
end

function ClassRef:setData(isOnEquip,crystalId,crystalUid,equipUid)
    self._data.isOnEquip = isOnEquip
    self._data.itemId = crystalId
    self._data.itemUid = crystalUid
    self._data.equipUid = equipUid
    self:refresh()
end

return ClassRef
